package com.ruoyi.module.service.impl;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.module.domain.ExamQuestion;
import com.ruoyi.module.mapper.ExamQuestionMapper;
import com.ruoyi.module.service.IExamQuestionService;

/**
 * 试题Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class ExamQuestionServiceImpl implements IExamQuestionService
{
    @Autowired
    private ExamQuestionMapper examQuestionMapper;

    /**
     * 查询试题列表
     * 
     * @param examQuestion 试题
     * @return 试题
     */
    @Override
    public List<ExamQuestion> selectExamQuestionList(ExamQuestion examQuestion)
    {
        return examQuestionMapper.selectExamQuestionList(examQuestion);
    }

    /**
     * 根据试卷ID查询试题列表
     * 
     * @param paperId 试卷ID
     * @return 试题集合
     */
    @Override
    public List<ExamQuestion> selectExamQuestionListByPaperId(Long paperId)
    {
        return examQuestionMapper.selectExamQuestionListByPaperId(paperId);
    }

    /**
     * 随机查询试题（用于租户前台）
     * 随机返回40道题，如果不够40就返回试题总量的一半
     * 
     * @param paperId 试卷ID
     * @return 试题集合
     */
    @Override
    public List<ExamQuestion> selectRandomExamQuestions(Long paperId)
    {
        // 统计试题总数
        int totalCount = examQuestionMapper.countExamQuestionsByPaperId(paperId);
        
        if (totalCount == 0)
        {
            return Collections.emptyList();
        }
        
        // 计算需要返回的题目数量
        int limit;
        if (totalCount >= 40)
        {
            limit = 40;
        }
        else
        {
            // 如果不够40，返回试题总量的一半
            limit = totalCount / 2;
            if (limit == 0)
            {
                limit = 1; // 至少返回1道题
            }
        }
        
        // 随机查询
        return examQuestionMapper.selectRandomExamQuestions(paperId, limit);
    }

    /**
     * 根据ID查询试题详情
     * 
     * @param id 试题ID
     * @return 试题
     */
    @Override
    public ExamQuestion selectExamQuestionById(Long id)
    {
        return examQuestionMapper.selectExamQuestionById(id);
    }

    /**
     * 新增试题
     * 
     * @param examQuestion 试题
     * @return 结果
     */
    @Override
    public int insertExamQuestion(ExamQuestion examQuestion)
    {
        return examQuestionMapper.insertExamQuestion(examQuestion);
    }

    /**
     * 批量新增试题
     * 
     * @param examQuestionList 试题列表
     * @return 结果
     */
    @Override
    public int batchInsertExamQuestion(List<ExamQuestion> examQuestionList)
    {
        if (examQuestionList == null || examQuestionList.isEmpty())
        {
            return 0;
        }
        return examQuestionMapper.batchInsertExamQuestion(examQuestionList);
    }

    /**
     * 修改试题
     * 
     * @param examQuestion 试题
     * @return 结果
     */
    @Override
    public int updateExamQuestion(ExamQuestion examQuestion)
    {
        return examQuestionMapper.updateExamQuestion(examQuestion);
    }

    /**
     * 批量删除试题
     * 
     * @param ids 需要删除的试题ID
     * @return 结果
     */
    @Override
    public int deleteExamQuestionByIds(Long[] ids)
    {
        return examQuestionMapper.deleteExamQuestionByIds(ids);
    }

    /**
     * 删除试题信息
     * 
     * @param id 试题ID
     * @return 结果
     */
    @Override
    public int deleteExamQuestionById(Long id)
    {
        return examQuestionMapper.deleteExamQuestionById(id);
    }
}

